//
//  EventInvitationListViewController.m
//  SecretRoom
//
//  Created by Sitdhibhong Laokok on 9/5/54 BE.
//  Copyright 2554 App Tree. All rights reserved.
//

#import "EventInvitationListViewController.h"

#import "Invitees.h"

@implementation EventInvitationListViewController
@synthesize delegate;
@synthesize invitationList;
@synthesize invitationConfirmButton;
@synthesize invitationFetchedResultController = _invitationFetchedResultController;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"Invitations";
    self.navigationItem.rightBarButtonItem = self.invitationConfirmButton;
    
    NSError *error = nil;
    [self.invitationFetchedResultController performFetch:&error];
    if (error != nil) {
        NSLog(@"%s Error while fetching invitees list: %@", __PRETTY_FUNCTION__, [error localizedDescription]);
    }
    [self.tableView reloadData];
}

- (void)viewDidUnload
{
    [self setInvitationConfirmButton:nil];
    [self setInvitationList:nil];
    [self setDelegate:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[[self.invitationFetchedResultController sections] lastObject] numberOfObjects];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell...
    Invitees *invitee = [self.invitationFetchedResultController objectAtIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", invitee.firstName, invitee.lastName];
    
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tv cellForRowAtIndexPath:indexPath];
    Invitees *invitee = [self.invitationFetchedResultController objectAtIndexPath:indexPath];
    
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [invitationList removeObject:invitee];
    } else {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [invitationList addObject:invitee];
    }
}

- (void)dealloc {
    [invitationConfirmButton release];
    [invitationList release];
    [delegate release];
    [_invitationFetchedResultController release];
    [super dealloc];
}

- (IBAction)invitationConfirmButtonClicked:(id)sender 
{
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(setInvitees:)]) {
        [self.delegate setInvitees:self.invitationList];
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSFetchedResultsController *)invitationFetchedResultController
{
    if (_invitationFetchedResultController != nil) {
        return _invitationFetchedResultController;
    }
    
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    fetchRequest.entity = [NSEntityDescription entityForName:@"Invitees" inManagedObjectContext:[[SRConnectionControlCenter sharedInstance] manageObjectContext]];
    fetchRequest.sortDescriptors = [NSArray arrayWithObjects:[NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES], [NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES], nil];
    fetchRequest.predicate = nil;
    
    NSFetchedResultsController *fetchController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[[SRConnectionControlCenter sharedInstance] manageObjectContext] sectionNameKeyPath:nil cacheName:@"Invitees"];
    
    self.invitationFetchedResultController = fetchController;
    
    [fetchRequest release];
    [fetchController release];
    
    return _invitationFetchedResultController;
}
@end
